/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package tetris;

import java.awt.*;

/**
 *
 * @author CJ
 */
public class Straight_Block extends Block{

    public Straight_Block(int x, int y, int rate, char dir, int l, int w)
    {
        super(x, y, rate, dir, l, w);
        color = Color.blue;
    }

    public Straight_Block()
    {
        super();
        color = Color.blue;
    }

    /*
     * N = O
     *     O
     *     O
     *     O
     *
     * E = OOOO
     * */

    public void setX(int x, Board b)
    {
        if((dir == 'N' && (x + 10 > WIDTH || x < 0))
                ||(dir == 'E' && (x + 40 > WIDTH || x < 0)))
            return;
        if(x > this.x)
        {
            //User wants to move to the right;
            if(!b.move(new Straight_Block(this.x + 10, y - 10, (int)rate, dir, LENGTH, WIDTH)))
                return;
        }
        else
        {
            //User wants to move to the left
            if(!b.move(new Straight_Block(this.x - 10, y - 10, (int)rate, dir, LENGTH, WIDTH)))
                return;
        }

        this.x = x;
        makearea();
    }

    public void rotate()
    {
        if(dir == 'N')
            dir = 'E';
        else if(dir == 'E')
            dir = 'N';

        if(dir == 'E' && x + 40 > WIDTH)
            x-=20;
        makearea();
    }

    public void draw(Graphics g, Board b)
    {
        if(!stopped)
            shadowDraw(g, b);
        g.setColor(color);

        if(dir == 'N')
        {
            g.fillRect(x, y, 10, 10);
            g.fillRect(x, y+10, 10, 10);
            g.fillRect(x, y+20, 10, 10);
            g.fillRect(x, y+30, 10, 10);

            g.setColor(Color.black);
            g.drawRect(x, y, 10, 10);
            g.drawRect(x, y+10, 10, 10);
            g.drawRect(x, y+20, 10, 10);
            g.drawRect(x, y+30, 10, 10);
        }
        else if(dir == 'E')
        {
            g.fillRect(x, y, 10, 10);
            g.fillRect(x+10, y, 10, 10);
            g.fillRect(x+20, y, 10, 10);
            g.fillRect(x+30, y, 10, 10);

            g.setColor(Color.black);
            g.drawRect(x, y, 10, 10);
            g.drawRect(x+10, y, 10, 10);
            g.drawRect(x+20, y, 10, 10);
            g.drawRect(x+30, y, 10, 10);
        }
    }

    public void makearea()
    {
        if(dir == 'N')
        {
            boundarea = new int[][]{
                {y/10 + 1, x/10},
                {y/10 + 2, x/10},
                {y/10 + 3, x/10},
                {y/10 + 4, x/10}};
        }
        else if(dir == 'E')
        {
            boundarea = new int[][]{
                {y/10 + 1, x/10},
                {y/10 + 1, x/10 + 1},
                {y/10 + 1, x/10 + 2},
                {y/10 + 1, x/10 + 3}};
        }
    }

    public boolean canMoveY(Board b)
    {
        if((dir == 'N' && y + 40 >= LENGTH)
                || (dir == 'E' && y + 10 >= LENGTH)
                || !b.move(this))
        {
            b.put(this);
            return false;
        }
        return true;
    }

    public void shadowDraw(Graphics g, Board b)
    {
        Straight_Block theBlock = new Straight_Block(x, y, (int)rate, dir, LENGTH, WIDTH);

        while(((dir == 'N' && theBlock.getY() + 40 < LENGTH)
                || (dir == 'E' && theBlock.getY() + 10 < LENGTH))
                && b.move(theBlock))

        {
            theBlock.setY(theBlock.getY() + 10);
        }

        int y = theBlock.getY();

        g.setColor(Color.black);

        switch(dir)
        {
            case 'N':
                g.drawPolygon(new int[]{x, x + 10, x+ 10, x},
                        new int[]{y, y, y+40, y+40},
                        4);
                break;
            case 'E':
                g.drawPolygon(new int[]{x, x+40, x+40, x},
                        new int[]{y, y, y+10, y+10},
                        4);
                break;
        }
        /*
         * N = O
         *     O
         *     O
         *     O
         *
         * E = OOOO
         * */
    }

}
